// myCourse_package/pages/confirmOrders/confirmOrders.ts

import {payment} from '../../../api/myCourse'
import {getSeriesDetail} from '../../../api/recommedCourse'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:'',
    studentSelectList:[], //学员选中切换
    orderCourseInfo:{
    }, //课
    studentList:[],
    paymentWays:[
      {id:1,label:"微信",checked:false},
      {id:2,label:"现金",checked:false},
      {id:3,label:"POS",checked:false},
    ],
    // 订单用户信息
    orderUserInfo:{
      username:"", //用户姓名
      studentName:"", //学生姓名
      stuId:"",
      phone:"" //联系方式
    },
    serialId:"", //系列课程id
    orderInfo:{

    }
  },
//  支付订单
async pay(){
  const flag = this.data.paymentWays.every(val=>{
    return val.checked === false
  })
  if(flag) return wx.Utils.toast("请选择支付方式")
 if(!flag){
  const payStatus = this.data.paymentWays.filter(val=>{
    return val.checked
  })[0].id
   
   switch(payStatus){
     case 1:
      //  微信支付
      this.wxChatPayment()
      break;
     case 2:
      //  现金支付
      this.generateOrder('2').then((res:any)=>{
        if(res.code === 200){
          wx.navigateTo({
            url:"/notify_package/pages/paymentNotice/paymentNotice"
          })
        }else{
          wx.Utils.toast("下单失败！")
        }
      })

       break;
      case 3:
      //POS支付
      this.generateOrder('3').then((res:any)=>{
        if(res.code === 200){
          wx.navigateTo({
            url:"/notify_package/pages/paymentNotice/paymentNotice"
          })
        }else{
          wx.Utils.toast("下单失败！")
        }
      })

        break;
   }

 }
},
// 生成订单
generateOrder(paymentStatus:string){
return new Promise(async(resolve,reject)=>{
  const info = this.data.orderCourseInfo as any
   const orderUserInfo = this.data.orderUserInfo as any 
   const {code,data:payInfo} = await payment({
    items:[
     {
      courseSerialId:info.courseSerialId
     }
    ],
    studentId:orderUserInfo.stuId,
    payment:paymentStatus,
    telephone: orderUserInfo.phone,
    userName:orderUserInfo.username
  })
  if(code === 200){  
    resolve(payInfo)
  }else{
    reject()
  }
})
},
// 微信支付
 wxChatPayment(){
  this.generateOrder('1').then(async(res:any)=>{
    const {timeStamp,nonceStr,packageVal:pkg,signType,paySign} = res.data
    try {
      const res = await wx.Utils.requestPayment(timeStamp,nonceStr,pkg,signType,paySign)  
       if(res === 'success'){
        this.setData({
          show:true
        })
       }
    } catch (error) {
    }
  })

},
// 选择支付方式
paymentWays(e:any){
const id = e.currentTarget.dataset.id
const  paymentWays = this.data.paymentWays as any
paymentWays.forEach((val:any)=>{
  if(val.id === id){
    val.checked = !val.checked
  }else{
    val.checked =false
  }
})
this.setData({
  paymentWays
})

},
 // 获取订单课程信息
 async getOrderInfo(){
   const serialId:number = this.data.serialId as any
 const {code,data:res} = await getSeriesDetail({
     serialId
 })
 if(code === 200){
   this.setData({
    orderCourseInfo:res
   })
 }
},
// 支付成功回调
conpletePayment(){
  this.setData({
    show:false
  })
  wx.navigateTo({
   url:`/myCourse_package/pages/myCourse/myCourse`
 }) 
 },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(data:any) {
    const userInfo = getApp().getUserInfo()
    const orderUserInfo = this.data.orderUserInfo
    orderUserInfo.username = userInfo.nickName
    orderUserInfo.phone = userInfo.phonenumber
    orderUserInfo.studentName = data.userName
    orderUserInfo.stuId = data.stuId
    
    this.setData({
      orderUserInfo,
      serialId:data.serialId,
      type:data.type,
      orderInfo:data.type === 'detail' ? JSON.parse(data.orderInfo) : {}
    })
    this.getOrderInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})